use tonic::{transport::Server, Request, Response, Status};

use bridge::bridge_server::{Bridge, BridgeServer};
use bridge::{BridgeResult, StartRequest};
pub mod bridge {
    tonic::include_proto!("bridge"); // The string specified here must match the proto package name
}
use crate::dev::dev_services;
#[derive(Debug, Default)]
pub struct MyBridge {}

// 这个服务是阻塞的
#[tonic::async_trait]
impl Bridge for MyBridge {
    async fn pass(&self, request: Request<StartRequest>) -> Result<Response<BridgeResult>, Status> {
        println!("Got a request: golang check...");
        // 测试可以执行任何函数
        dev_services();
        let reply = bridge::BridgeResult {
            message: format!("Hello {}!", request.into_inner().id).into(), // We must use .into_inner() as the fields of gRPC requests and responses are private
        };
        Ok(Response::new(reply)) // Send back our formatted greeting
    }
}

pub async fn mrpc_init() {
    let addr = "[::1]:50001".parse().unwrap();
    let greeter = MyBridge::default();
    match Server::builder()
        .add_service(BridgeServer::new(greeter))
        .serve(addr)
        .await
    {
        Ok(_) => {}
        Err(err) => panic!("发生错误： {}", err),
    }
}
